﻿//****************************************************************************
// Copyright (c) Microsoft Corporation. All rights reserved.
//****************************************************************************
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using Microsoft.FxCop.Sdk;
using System.Linq;

namespace Microsoft.FxCop.Rules.ASP.Security
{
    class ValidateRequestShouldBeEnabled : BasePageIntrospectionRule
    {
        #region *** Constructor ***
        public ValidateRequestShouldBeEnabled()
            : base("ValidateRequestShouldBeEnabled")
        {
        }
        #endregion

        #region *** Methods ***

        #region *** BasePageRule ***
        protected override void CheckFrameworkInitialized(Method frameworkInit)
        {
            // If the ValidateRequest is disable, the FrameworkInitialize method
            // will not call Request.ValidateInput() method.
            // The page directive cannot be set in the Page_Init process.

            var ins = (from p in frameworkInit.Instructions
                       where p.OpCode == OpCode.Callvirt && ((p.Value as Method).Name.Name == "ValidateInput")
                       select p).Count();

            if (ins == 0)
                ReportProblem();
        }
        #endregion

        #endregion
    }
}
